package org.dawfinn.droiditarian.view;

import java.io.IOException;

import org.dawfinn.droiditarian.R;
import org.dawfinn.droiditarian.data.DeedLocation;
import org.dawfinn.droiditarian.reportpicupload.InternetReportPicUpload;
import org.dawfinn.droiditarian.reportpicupload.ReportPicUpload;

import com.google.protobuf.ByteString;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MapItemView extends RelativeLayout {
	
	public static final String PIC_KEY = "pic";
	public static final String LOC_SERVER_KEY = "serverKey";
	
	private final DeedLocation location;
	private final Context context;
	private TextView city;
	private ImageView picture;
	private ImageView reportButton;
	
	public MapItemView(Context context, DeedLocation location, String cityText) {
		super(context);
		this.context = context;
		this.location = location;

		LayoutInflater inflater = (LayoutInflater) context
			.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.map_item_view_with_report, this, true);
		city = (TextView)findViewById(R.id.deedLoc);
		picture = (ImageView)findViewById(R.id.locationPicture);
		reportButton = (ImageView)findViewById(R.id.reportButton);
		
		setPicture(location.getPicture());
		city.setText(cityText);
	}

	private void setPicture(ByteString bmpBytes) {
		if (bmpBytes != null) {
			picture.setImageBitmap(BitmapFactory.decodeByteArray(bmpBytes.toByteArray(), 0, bmpBytes.size()));
			reportButton.setVisibility(View.VISIBLE);
			reportButton.setOnClickListener(new View.OnClickListener() {

				public void onClick(View arg0) {
					new ReportDialog(context, R.style.DroiditarianTheme).show();
				}
			});
		} else {
			picture.setVisibility(View.GONE);
			reportButton.setVisibility(View.GONE);
		}
	}
	
	class ReportDialog extends AlertDialog {

		protected ReportDialog(Context context, int theme) {
			super(context, theme);
			this.setIcon(R.drawable.red_flag);
			this.setTitle(context.getString(R.string.report_title_text));
			this.setView(new ReportView(context));
			this.setButton(AlertDialog.BUTTON_NEGATIVE,
					context.getString(R.string.cancel_button),
					new ReportDialogListener());
			this.setButton(AlertDialog.BUTTON_POSITIVE, 
					context.getString(R.string.report_button),
					new ReportDialogListener());
		}
	}
	
	class ReportDialogListener implements DialogInterface.OnClickListener {

		public void onClick(DialogInterface view, int which) {
			if (which == DialogInterface.BUTTON_NEGATIVE)
				view.cancel();
			else
				new ReportPicAsyncTask().execute();
		}
	}
	
	class ReportPicAsyncTask extends AsyncTask<Void, Void, Boolean> {
		
		@Override
		protected Boolean doInBackground(Void... deedLocKeys) {
			// The next line is for testing only should remain commented out otherwise
			// ReportPicUpload report = new LocalReportPicUpload();
			ReportPicUpload report = new InternetReportPicUpload();
			try {
				String key = location.getKey();
				if (key != null) {
					report.reportDeedLocationPic(location.getKey());
					return true;
				} else {
					return false;
				}
			} catch (IOException e) {
				return false;
			}
		}
		
		@Override
		protected void onPostExecute(Boolean success) {
			int toastMessageId;
			if (success) {
				toastMessageId = R.string.report_success;
				picture.setVisibility(View.GONE);
				reportButton.setVisibility(View.GONE);
				location.removePicture();
			} else {
				toastMessageId = R.string.report_fail;
			}
			Toast.makeText(context, context.getString(toastMessageId), Toast.LENGTH_LONG).show();
		}
	} 
}
